<template>
  <div class="ele-body">


   
    <el-card shadow="never">
      <el-row :gutter="31" style="margin-bottom: 20px;">
        <el-col :sm="12" :xs="12" :md="5" :span="5">
          <div class="piece-all purple-bj flex-align-center">
            <el-avatar :size="43" class="icon-right" :src="require('@/assets/indexs/1.png')" />
            <div class="flex-columns piece-text">
              <div>{{all}}</div>
              <span>总数量</span>
            </div>
          </div>
        </el-col>
        <el-col :sm="12" :xs="12" :md="5" :span="5">
          <div class="piece-all blue-bj flex-align-center">
            <el-avatar :size="43" class="icon-right" :src="require('@/assets/indexs/2.png')" />
            <div class="flex-columns piece-text">
              <div>{{get}}</div>
              <span>已划拨</span>
            </div>
          </div>
        </el-col>
        <el-col :sm="12" :xs="12" :md="4" :span="4">
          <div class="piece-all yellow-bj flex-align-center">
            <el-avatar :size="43" class="icon-right" :src="require('@/assets/indexs/3.png')" />
            <div class="flex-columns piece-text">
              <div>{{unget}}</div>
              <span>未划拨</span>
            </div>
          </div>
        </el-col>
        <el-col :sm="12" :xs="12" :md="5" :span="5">
          <div class="piece-all green-bj flex-align-center">
            <el-avatar :size="43" class="icon-right" :src="require('@/assets/indexs/4.png')" />
            <div class="flex-columns piece-text">
              <div>{{used}}</div>
              <span>已激活</span>
            </div>
          </div>
        </el-col>
        <el-col :sm="12" :xs="12" :md="5" :span="5">
          <div class="piece-all red-bj flex-align-center">
            <el-avatar :size="43" class="icon-right" :src="require('@/assets/indexs/5.png')" />
            <div class="flex-columns piece-text">
              <div>{{unuse}}</div>
              <span>未激活</span>
            </div>
          </div>
        </el-col>
      </el-row>
      <!-- 搜索表单 -->
      <card-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新增
          </el-button> 
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-_transfer"
            class="ele-btn-icon"
            @click="openTransfer(1)"
          >
           划拨
          </el-button>
          <el-button
            size="small"
            type="info"
            icon="el-icon-_save"
            class="ele-btn-icon"
            @click="onExport"
          >
           导出
          </el-button>
        </template>



     
   

        <template v-slot:account="{ row }">
          <div class="ele-cell" v-if="row.figureAccount">
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.figureAccount.telphone }} </div>
            </div>
          </div>
          <span v-else>--</span>
          
        </template>

        <template v-slot:transfer="{ row }">
          <el-tag v-if="row.account_id" type="success" effect="plain">已划拨</el-tag>
          <el-tag v-else type="info" effect="plain">未划拨</el-tag>
        </template>


        <template v-slot:is_status="{ row }">
          <el-tag v-if="row.is_status==1" type="info" effect="plain">待激活</el-tag>
          <el-tag v-if="row.is_status==2" type="success" effect="plain">已激活</el-tag>
        </template>


        <template v-slot:use_time="{ row }">

          <span v-if="row.use_time">{{ row.use_time }}</span>
          <span v-else>--</span>
          
        </template>





        <!-- 状态列 -->
        <template v-slot:is_display="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="row.is_display"
            @change="editStatus(row)"
          />
        </template>


        <template v-slot:sort>
          <i class="sort-handle el-icon-rank ele-text-secondary" style="cursor: move;"></i>
        </template>

        
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
         
          <el-popconfirm
            v-if="row.account_id && row.is_status==1"
            class="ele-action"
            title="确定要收回此卡密吗？"
            @confirm="retrieve(row)"
          >
            <template v-slot:reference>
              <el-link type="warning" :underline="false" icon="el-icon-_back">
                收回
              </el-link>
            </template>
          </el-popconfirm>

          <el-link
            v-if="!row.account_id"
            type="success"
            :underline="false"
            icon="el-icon-_transfer"
            @click="openTransfer(2,row)"
          >
            划拨
          </el-link>
          <el-link

            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <card-edit :type="type" :visible.sync="showEdit" :data="current"  @done="reload"/>
    <transfer :type="transferType" :visible.sync="showTransfer" :data="current"  @done="reload"/>
  </div>
</template>

<script>
  import VueCountUp from 'vue-countup-v2';
  import CardSearch from './components/card-search';
  import CardEdit from './components/card-edit';
  import Transfer from './components/transfer';
  import { list, remove, statis,retrieve  } from '@/api/card/list';
  import { utils, writeFile } from 'xlsx';

  export default {
    name: 'Card',
    components: {
      CardSearch,
      CardEdit,
      VueCountUp,
      Transfer
    },
    data() {
      return {
        type:1,
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
          {
            prop: 'speaker_id',
            label: '声音ID',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
          },
          {
            prop: 'card',
            label: '卡密',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
          },
          {
            prop: 'account',
            label: '开发者',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'account',
          },
          {
            prop: 'transfer',
            label: '划拨状态',
            align: 'center',
            width: 150,
            resizable: false,
            slot: 'transfer',
            showOverflowTooltip: true
          },
          {
            prop: 'is_status',
            label: '激活状态',
            align: 'center',
            width: 150,
            resizable: false,
            slot: 'is_status',
            showOverflowTooltip: true
          },
          {
            prop: 'due_time',
            label: '到期时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
          },
          {
            prop: 'use_time',
            label: '激活时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'use_time'
          },
          {
            prop: 'create_time',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (row, column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 260,
            align: 'center',
            resizable: false,
            slot: 'action',
            hideInSetting: true,
            showOverflowTooltip: true
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示分配回收员
        showTransfer: false,
        //弹框名称
        id:0,
        where:{
          name:1,
          is_status:'',
          nickname:'',
        },
        exportData:[],
        all:0,
        unget:0,
        get:0,
        unuse:0,
        used:0,
        transferType :0,
      };
    },
    methods: {
      onExport() {

          console.log(this.where);

          list(this.where).then((data) => {
            this.exportData = data.list;

            let array = [
              ['开发者','卡密','到期时间', '状态', '激活时间']
            ];

            


            this.exportData.forEach((d, i) => {

                if(d.is_status == 1){
                  d.is_status = '未激活';
                }else if(d.is_status == 2){
                  d.is_status = '已激活';
                }

                if(!d.figureAccount || typeof d.figureAccount  === 'undefined' ||  typeof d.figureAccount.telphone === 'undefined' || d.figureAccount.telphone == null || d.figureAccount.telphone=== ''){
                  d.account = '无';
                }else{
                  d.account =  d.figureAccount.telphone ;
                }

                if( d.use_time  === 'undefined' || d.use_time== null || d.use_time === ''){
                  d.use_time = '无';
                }

                array.push([d.account, d.card, d.due_time, d.is_status, d.use_time]);
            });
            const sheet = utils.aoa_to_sheet(array);
            // 如果需要设置列宽可以这样写
            sheet["!cols"] = [
                { wch: 20 },
                { wch: 30 },
                { wch: 30 },
                { wch: 10 },
                { wch: 20 },
                { wch: 20 },
                { wch: 20 },
                { wch: 20 }
            ];
            writeFile(
                {
                    SheetNames: ['Sheet1'],
                    Sheets: {
                        Sheet1: sheet
                    }
                },
                '卡密列表.xlsx'
            );

          }).catch((e) => {
            this.$message.error(e.message);
          });


          
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        where = where ? where : {};
        where.type = this.type;
        this.where = where;
        return list({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload(where) {
        where = where ? where : {};
        where.type = this.type;
        this.where = where;
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 打开编辑弹窗 */
      openTransfer(type,row) {
        this.transferType = type;
        this.current = row;
        this.showTransfer = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        remove(row.id).then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
      },

      retrieve(row) {
        const loading = this.$loading({ lock: true });
        retrieve({id:row.id}).then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的数据吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        }).catch(() => {});
      },
      
      dataStatis(){
        statis().then((data) => {
          if(data != null){
         
            this.all = data.all; 
            this.unget = data.unget;   
            this.get = data.get;  
            this.unuse = data.unuse;   
            this.used = data.used;  

          }
          
        }).catch((e) => {
          this.$message.error(e.message);
        });
      },
    },mounted() {
      this.dataStatis();
    },
    watch: {
      $route: {
        handler(route) {
          this.type = route.query.type;
        },
        immediate: true
      },
      type(){
        this.reload();
      }
    },
  };
</script>

<style lang="scss" scoped>

  .drag {
    background: #000 !important;
    background-image: linear-gradient(#333, #999) !important;
  }

  .ele-cell-content {
   
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .icon {
    width: 32px;
    height: 32px;
  }

  .singlePalaceGrid {
    flex: 50%;
    height: 93px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding-left: 28px;
  }
  .flex-column {
      flex-direction: column;
      justify-content: center;
  }
  .singlePalaceGrid .accumulateNum {
    margin-left: 8px;
    height: 62px;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
  }
  .singlePalaceGrid .accumulateNum .Num {
    color: #1e6ceb;
    font-size: 20px;
  }

  .flex-align-center {
    display: flex;
    align-items: center;
  }
 
  .purple-bj{
    background-color: #EEE7FF;
  }
  .blue-bj{
    background-color: #E2F6F6;
  }
  .yellow-bj{
    background-color: #FFF0E1;
  }
  .green-bj{
    background-color: #E4FFD5;
  }
  .red-bj{
    background-color: #FFD8D8;
  }
  .piece-all{
    margin-top: 19px;
    border-radius: 10px;
    height: 94px;
    padding: 0 11px;
    .icon-right{
      margin-right: 12px;
    }
    .piece-text{
      div{
        color: #333;
        font-size: 30px;
        font-weight: bold;
      }
      span{
        color: #666;
        font-size: 16px;
      }
    }
  }
</style>